{***************************************************************
    SpyGestion  Copyright (C) 2008 Christophe KOALI <nature2gas@gmail.com>
    
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.
    
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
    
    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
    
****************************************************************}
unit frmFLX;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, JvWizard, JvExControls, JvComponent, StdCtrls, JvExStdCtrls,
  JvRichEdit, modFileVerif, ExtCtrls;

const
   WIZARD_LICENCE_MODAL_RES_FINISH     = 0;
   WIZARD_LICENCE_MODAL_RES_CANCEL = 1;


type
  TformFLX = class(TForm)
    wizardFLX: TJvWizard;
    wizWelcome: TJvWizardWelcomePage;
    wizLicence: TJvWizardInteriorPage;
    wizArticlesImportants: TJvWizardInteriorPage;
    wizAvezVousAchete: TJvWizardInteriorPage;
    optOUI: TRadioButton;
    optNON: TRadioButton;
    JvWizardInteriorPage4: TJvWizardInteriorPage;
    wizFinished: TJvWizardInteriorPage;
    reLicence: TJvRichEdit;
    reArticlesImportants: TJvRichEdit;
    lblCantBeSell: TLabel;
    cmdNextGo: TTimer;
    wizLicenceInvalide: TJvWizardInteriorPage;
    reArnaque: TJvRichEdit;
    procedure wizAvezVousAcheteNextButtonClick(Sender: TObject;
      var Stop: Boolean);
    procedure FormCreate(Sender: TObject);
    procedure wizLicenceEnterPage(Sender: TObject;
      const FromPage: TJvWizardCustomPage);
    procedure wizLicenceExitPage(Sender: TObject;
      const FromPage: TJvWizardCustomPage);
    procedure cmdNextGoTimer(Sender: TObject);
    procedure wizardFLXFinishButtonClick(Sender: TObject);
    procedure wizWelcomeNextButtonClick(Sender: TObject; var Stop: Boolean);
    procedure wizardFLXCancelButtonClick(Sender: TObject);
  private
    { Private declarations }
    FState: integer;

    // Date de generation de la licence
    FLicenceDate: int64;

    // Definit si la licence est valide
    FInValidLicenceFile: boolean;

  public
    { Public declarations }
    property IsInvalideFiles: boolean read FInValidLicenceFile;
    property LicenceDate: int64 read  FLicenceDate;

    procedure StartWizard;
  end;

  function IsValideLicenceFile: boolean;



var
  formFLX: TformFLX;

implementation

uses modConstantes, modInteractionKey, gnugettext;

{$R *.dfm}

function IsValideLicenceFile: boolean;
var AppPath: string;
    CurrFile: TFichierTextSignature;
begin

    Result := false;

          AppPath := IncludeTrailingPathDelimiter(ExtractFileDir(Application.ExeName));


          //
          // Licence
          //
          CurrFile := TFichierTextSignature.Create;

          if (CurrFile.LoadAndCheck(AppPath + CNT_FILE_LICENCE_RTF, RSA_GetPublicKey) = true) then begin
             Result := true;

          end;
          CurrFile.Free;

end;

procedure TformFLX.StartWizard;
begin
     wizardFLX.SelectFirstPage;
     ShowModal;
end;

procedure TformFLX.cmdNextGoTimer(Sender: TObject);
var AppPath: string;
    CurrFile: TFichierTextSignature;
begin
    Inc(FState);

    if FState = 2 then begin
       cmdNextGo.Interval := 0;





          AppPath := IncludeTrailingPathDelimiter(ExtractFileDir(Application.ExeName));


          //
          // Licence
          //
          CurrFile := TFichierTextSignature.Create;

          if (CurrFile.LoadAndCheck(AppPath + CNT_FILE_LICENCE_RTF, RSA_GetPublicKey) = true) then begin
             reLicence.Lines.LoadFromStream(Currfile.FichierStream);

             FLicenceDate := Currfile.FichierDateGen;

          end else begin
             FInValidLicenceFile := true;
          end;
          CurrFile.Free;



          //
          // Articles
          //
          CurrFile := TFichierTextSignature.Create;

          if (CurrFile.LoadAndCheck(AppPath + CNT_FILE_ARTICLES_RTF, RSA_GetPublicKey) = true) then begin
             reArticlesImportants.Lines.LoadFromStream(Currfile.FichierStream);

          end else begin
             FInValidLicenceFile := true;
          end;
          CurrFile.Free;



          //
          // Arnaque
          //
          CurrFile := TFichierTextSignature.Create;

          if (CurrFile.LoadAndCheck(AppPath + CNT_FILE_ARNAQUE_RTF, RSA_GetPublicKey) = true) then begin
             reArnaque.Lines.LoadFromStream(Currfile.FichierStream);

          end else begin
             FInValidLicenceFile := true;
          end;
          CurrFile.Free;

       

       wizWelcome.VisibleButtons := wizWelcome.VisibleButtons + [bkNext];


    end;

end;

procedure TformFLX.FormCreate(Sender: TObject);

begin

    FState := 0;
    cmdNextGo.Interval := 150;
    wizWelcome.VisibleButtons := wizWelcome.VisibleButtons - [bkNext];

    FLicenceDate := 0;
    FInValidLicenceFile := false;



    //reLicence.Lines.LoadFromFile(AppPath + CNT_FILE_LICENCE_RTF);

    // reArticlesImportants
end;

procedure TformFLX.wizardFLXCancelButtonClick(Sender: TObject);
begin
    ModalResult := WIZARD_LICENCE_MODAL_RES_CANCEL;
end;

procedure TformFLX.wizardFLXFinishButtonClick(Sender: TObject);
begin
     if FInValidLicenceFile then begin
        Application.Terminate;

     end else begin
        Self.ModalResult := WIZARD_LICENCE_MODAL_RES_FINISH;
        Self.CloseModal;
        Self.Close;
     end;
end;

procedure TformFLX.wizAvezVousAcheteNextButtonClick(Sender: TObject;
  var Stop: Boolean);
begin
    if optNON.Checked then begin
       wizardFLX.ActivePage := wizFinished;
       Stop := true;
    end;

end;

procedure TformFLX.wizLicenceEnterPage(Sender: TObject;
  const FromPage: TJvWizardCustomPage);
begin
     wizardFLX.ButtonNext.Caption := _('I Agree');
end;

procedure TformFLX.wizLicenceExitPage(Sender: TObject;
  const FromPage: TJvWizardCustomPage);
begin
     wizardFLX.ButtonNext.Caption := _('&Next >');

end;

procedure TformFLX.wizWelcomeNextButtonClick(Sender: TObject;
  var Stop: Boolean);
begin
    if NoT(FInValidLicenceFile) then begin
       wizardFLX.ActivePage := wizLicence;
       Stop := true;
    end;

end;

end.
